Trò chơi Angry Birds trong UNITY Engine
31.648 lượt xem;
- Vector3PathTweenProperty.cs
- genericTypes /
- properties /
- GoKit /
- Plugins /
- Assets /
- project /
1 using UnityEngine;
2 using System;
3 using System.Collections;
4
5
6 public class Vector3PathTweenProperty : AbstractTweenProperty, IGenericProperty
7 {
8 public string propertyName { get; private set; }
9
10 private Action<Vector3> _setter;
11 private GoSpline _path;
12 private Vector3 _startValue;
13
14
15 public Vector3PathTweenProperty( string propertyName, GoSpline path, bool isRelative = false ) : base( isRelative )
16 {
17 this.propertyName = propertyName;
18 _path = path;
19 }
20
21
22 /// <summary>
23 /// validation checks to make sure the target has a valid property with an accessible setter
24 /// </summary>
25 public override bool validateTarget( object target )
26 {
27 // cache the setter
28 _setter = GoTweenUtils.setterForProperty<Action<Vector3>>( target, propertyName );
29 return _setter != null;
30 }
31
32
33 public override void prepareForUse()
34 {
35 // if this is a from tween first reverse the path then build it. we unreverse in case we were copied
36 if( _ownerTween.isFrom )
37 _path.reverseNodes();
38 else
39 _path.unreverseNodes();
40
41 _path.buildPath();
42
43 // a from tween means the start value is the last node
44 if( _ownerTween.isFrom )
45 {
46 _startValue = _path.getLastNode();
47 }
48 else
49 {
50 // retrieve the getter only when needed
51 var getter = GoTweenUtils.getterForProperty<Func<Vector3>>( _ownerTween.target, propertyName );
52 _startValue = getter();
53 }
54 }
55
56
57 public override void tick( float totalElapsedTime )
58 {
59 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
60 var vec = _path.getPointOnPath( easedTime );
61
62 // if we are relative, add the vec to our startValue
63 if( _isRelative )
64 vec += _startValue;
65
66 _setter( vec );
67 }
68
69 }
2 using System;
3 using System.Collections;
4
5
6 public class Vector3PathTweenProperty : AbstractTweenProperty, IGenericProperty
7 {
8 public string propertyName { get; private set; }
9
10 private Action<Vector3> _setter;
11 private GoSpline _path;
12 private Vector3 _startValue;
13
14
15 public Vector3PathTweenProperty( string propertyName, GoSpline path, bool isRelative = false ) : base( isRelative )
16 {
17 this.propertyName = propertyName;
18 _path = path;
19 }
20
21
22 /// <summary>
23 /// validation checks to make sure the target has a valid property with an accessible setter
24 /// </summary>
25 public override bool validateTarget( object target )
26 {
27 // cache the setter
28 _setter = GoTweenUtils.setterForProperty<Action<Vector3>>( target, propertyName );
29 return _setter != null;
30 }
31
32
33 public override void prepareForUse()
34 {
35 // if this is a from tween first reverse the path then build it. we unreverse in case we were copied
36 if( _ownerTween.isFrom )
37 _path.reverseNodes();
38 else
39 _path.unreverseNodes();
40
41 _path.buildPath();
42
43 // a from tween means the start value is the last node
44 if( _ownerTween.isFrom )
45 {
46 _startValue = _path.getLastNode();
47 }
48 else
49 {
50 // retrieve the getter only when needed
51 var getter = GoTweenUtils.getterForProperty<Func<Vector3>>( _ownerTween.target, propertyName );
52 _startValue = getter();
53 }
54 }
55
56
57 public override void tick( float totalElapsedTime )
58 {
59 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
60 var vec = _path.getPointOnPath( easedTime );
61
62 // if we are relative, add the vec to our startValue
63 if( _isRelative )
64 vec += _startValue;
65
66 _setter( vec );
67 }
68
69 }